<?php

class Address extends Controller
{
	public function __construct()
	{
		parent::Controller();
		
		// Load libraries
		$this->load->database();

		// Load helpers
		$this->load->helper('url');
			
		// Load models
		$this->load->model('address_model', 'post');
	}
	
	public function index()
	{	
		// Get data from model
		$data['posts'] = $this->post->getAll();
		
		// Load views
		$this->load->view('header');
		$this->load->view('index', $data);
		$this->load->view('address', $data);
		$this->load->view('footer');
	}

	public function read()
	{
		// Get id from uri
		$address_id = $this->uri->segment(3);
		
		// Get data from model
		$data['post'] = $this->post->getById($address_id);
		
		// Load views
		$this->load->view('header');
		$this->load->view('read', $data);
		$this->load->view('footer');
	}
	
	public function create()
	{	
		if($_POST)
		{
			// Build post object
			$post = new Address_model();
			$post->address= $this->input->post('address', TRUE);
			$post->city = $this->input->post('city', TRUE);
			$post->district = $this->input->post('district', TRUE);
			$post->state = $this->input->post('state', TRUE);
			$post->pin = $this->input->post('pin', TRUE);
			$post->phone = $this->input->post('phone', TRUE);
			$post->mob = $this->input->post('mob', TRUE);
			
			
	
			// Save post to database
			if ($post->save($post)) {
				redirect(base_url(), 'location');
			}
		}
	
		// Load helpers
		$this->load->helper('form');
	
		// Initialize form
		$data['action'] = site_url('address/create');
		
		$data['address'] = NULL;
		$data['city'] = NULL;
		$data['district'] = NULL;
		$data['state'] = NULL;
		$data['pin'] = NULL;
		$data['phone'] = NULL;
		$data['mob'] = NULL;
		
		// Load views	
		$this->load->view('header');
		$this->load->view('upsert', $data);
		$this->load->view('footer');
	}
		
	public function update()
	{
		if ($_POST) 
		{
			// Build post object
			$post = new Address_model();
			$post->address_id = $this->uri->segment(3);
			$post->address = $this->input->post('address', TRUE);
			$post->city = $this->input->post('city', TRUE);
			$post->district = $this->input->post('district', TRUE);
			$post->state = $this->input->post('state', TRUE);
			$post->pin = $this->input->post('pin', TRUE);
			$post->phone = $this->input->post('phone', TRUE);
			$post->mob = $this->input->post('mob', TRUE);
						
			// Save post to database
			if ($post->save($post)) {
				redirect(base_url(), 'location');
			}
		}
		
		// Get post from database
		$address_id = $this->uri->segment(3);
		$post = $this->post->getById($address_id);
		
		// Initialize form
		$this->load->helper('form');
		$data['action'] = site_url('address/update/'.$address_id);
		$data['address'] = $post->address;
		$data['city'] = $post->city;
		$data['district'] = $post->district;
		$data['state'] = $post->state;
		$data['pin'] = $post->pin;
		$data['phone'] = $post->phone;
		$data['mob'] = $post->mob;
		
		
		

		// Load views	
		$this->load->view('header');
		$this->load->view('upsert', $data);
		$this->load->view('footer');
	}
	
	public function delete()
	{
		$post = new Address_model();
		$post->address_id = $this->uri->segment(3);
		if ($post->delete($post)) {
			redirect(base_url(), 'location');
		}
	}	
}